﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

namespace Moonlite.Modules.Editors.ImageViewer
{
    public abstract class ImageViewerLoader
    {
        private static List<ImageViewerLoader> imageLoaders = new List<ImageViewerLoader>();
        protected abstract string Extension { get; }
        public static void Register(ImageViewerLoader imageLoader)
        {
            if (imageLoaders.Contains(imageLoader))
            {
                throw new InvalidOperationException("ImageLoader is already registered.");
            }

            imageLoaders.Add(imageLoader);
        }

        public static string[] GetRegisteredExtensions()
        {
            List<string> extensions = new List<string>();
            foreach (ImageViewerLoader imageLoader in imageLoaders)
            {
                extensions.Add(imageLoader.Extension);
            }

            return extensions.ToArray();
        }

        public static Image LoadImage(string path)
        {
            string extension = Path.GetExtension(path);
            foreach (ImageViewerLoader imageLoader in imageLoaders)
            {
                if (extension == imageLoader.Extension)
                {
                    return imageLoader.LoadImageCore(path);
                }
            }

            return null;
        }

        protected abstract Image LoadImageCore(string path);
    }
}
